package middleware

import (
	"gateway-server/internal/logger"
	"gateway-server/pkg/beego"
	"time"
)

func RequestLogger() {
	beego.InsertGlobalFilterChain(func(ctx *beego.BeeCtx, next beego.BeeNextFunc) {
		// 获取客户端IP
		clientIP := ctx.Request.RemoteAddr
		if forwarded := ctx.Request.Header.Get("X-Forwarded-For"); forwarded != "" {
			clientIP = forwarded
		}

		// 开始计时
		start := time.Now()

		// 继续执行后续中间件或路由处理
		next(ctx)

		// 记录日志
		elapsed := time.Since(start)
		logger.Default.Infof("[Request] IP: %s | Method: %s | Path: %s | Time: %v",
			clientIP, ctx.Request.Method, ctx.Request.URL.Path, elapsed)
	})
}
